#include "StdAfx.h"
//-------------------------------------------------------------------------------------------------
//MxUtils.cpp : implementation of the MxUtils class       
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk July 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------

#include "MxUtils.h"

#include <stdio.h>


clock_t MxUtils::_startTime;
char	MxUtils::_tempBuff1[MxUtils::TempBuff1Len+1];

MxUtils::MxUtils(void)
{
	ResetClock();
}


MxUtils::~MxUtils(void)
{
}

void MxUtils::ResetClock()
{
	_startTime = clock();
}


const char *MxUtils::WideCharsToAscii_1(LPCTSTR text)
{
	if (text == '\0')
		strcpy(_tempBuff1, "(!!!null string!!!)");
	else
	{
		wcstombs(_tempBuff1, text, MxUtils::TempBuff1Len );
	}
	return _tempBuff1;
}



void MxUtils::OutputDebugMsg(const char *method, const char *printfStr, ...)
{
	char	buffer[MxUtils::DebugStringLen];

	clock_t now = clock();
	double ticks = (double)(now - _startTime) / CLOCKS_PER_SEC;

	int len = 0;
	int lenPreamble = 0;
	if (printfStr == '\0')
		len = _snprintf(buffer, sizeof(buffer), "[%7.3f] %s: (!!!format string was null!!!)", ticks, method);
	else
	{
		va_list argptr;
		va_start( argptr, printfStr );

		lenPreamble = _snprintf(buffer, sizeof(buffer), "[%7.3f] %s: ", ticks, method);
		if ( (lenPreamble >= sizeof(buffer)) || (lenPreamble < 0) )
			len = lenPreamble;
		else
		{
			int lenFormat = vsnprintf(&buffer[lenPreamble], MxUtils::DebugStringLen-lenPreamble, printfStr, argptr);
			if ( lenFormat <= 0 )
				len = sizeof(buffer);
			else
				len = lenPreamble + lenFormat;
		}
		va_end( argptr );
	}
	if ((len < (sizeof(buffer)-1)) && (len >= 0))
	{
		buffer[len] = '\n';
		buffer[len+1] = '\0';
	}
	else
	{
		buffer[sizeof(buffer)-5] = '.';
		buffer[sizeof(buffer)-4] = '.';
		buffer[sizeof(buffer)-3] = '.';
		buffer[sizeof(buffer)-2] = '\n';
		buffer[sizeof(buffer)-1] = '\0';
	}
    std::string msg1(buffer);

	std::wstring msg2(msg1.length(),L' ');
	std::copy(msg1.begin(), msg1.end(), msg2.begin());

	::OutputDebugString(msg2.c_str());
}

//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------
